<?php

namespace App\Http\Controllers\Client;

use App\Http\Controllers\Controller;
use App\Services\MemberService;
use App\Utils\ApiResponse;
use App\Utils\JwtUtil;
use Illuminate\Http\Request;

class ClientBase extends Controller
{
    protected $currentUser;

    protected $customParamJson;

    // 不验证token的路由
    private $whiteList = [
        'member.register',
        'member.login',
        'product.getProductList',
        'product.setLink',
    ];

    public function __construct(Request $request)
    {
        $route = $request->route();
        $currentRouteName = $route->getName();
        if (!in_array($currentRouteName, $this->whiteList)) {
            $token = $request->header('x-token');
            if ($token == "[object Null]") {
                ApiResponse::fail("token异常，请重新登录",403);
            }
            $tokenDecode = JwtUtil::checkToken($token);
            $memberService = new MemberService();
            $this->currentUser = $memberService->getMemberInfoByNumber($tokenDecode->data->user_number);
            if (empty($this->currentUser)) {
                ApiResponse::fail("账号异常，请联系管理员",403);
            }

            // 拼多多自定义参数，用于跟单
            $customParamData = [
                'uid'      => 1, // 推手，推广者，简单来说谁拿钱就是谁
                'memberId' => $this->currentUser->id
            ];
            $this->customParamJson = json_encode($customParamData);

            if (!$tokenDecode) {
                ApiResponse::fail("token异常，请重新登录",403);
            }
        }
    }
}
